package C630;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: javase
 * @description:
 * @author: luolidong
 * @create: 2021-06-30 11:40
 * @version: 1.0
 */
public class MapTest {
    private static Map<String, Object> map =new HashMap<>();

    private MapTest(){

    }
    public static Object getInstance(String clazz) throws Exception{
        if(map.isEmpty()){
            Class<?> aClass = Class.forName(clazz);
            Object o = aClass.newInstance();
            map.put(clazz, o);
        }
        return map.get(clazz);
    }
}
class Test2{
    public static void main(String[] args) throws Exception {
//        Person person = (Person) MapTest.getInstance("C630.Person");
//        person.setId(12);
//        Person person2 = (Person) MapTest.getInstance("C630.Person");
//        Person person3 = (Person) MapTest.getInstance("C630.Person");
//        System.out.println("person==person2 = " +( person == person2));
//        System.out.println("person3.getId() = " + person3.getId());

        Person2 person = (Person2) MapTest.getInstance("C630.Person2");
        person.setId(12);
        Person2 person2 = (Person2) MapTest.getInstance("C630.Person2");
        Person2 person3 = (Person2) MapTest.getInstance("C630.Person2");
        System.out.println("person==person2 = " +( person == person2));
        System.out.println("person3.getId() = " + person3.getId());
    }
}
